import { createVNode, render, h } from 'vue';
import condition from './module/condition.vue';
import { v4 as uuidv4 } from 'uuid';

export default function registerCondition(lf) {
  lf.register('bpmn:condition', ({ HtmlNode, HtmlNodeModel }) => {
    class ConnectNode extends HtmlNode {
      setHtml(rootEl) {
        const { properties } = this.props.model;
        const el = document.createElement('div');
        const ssbi = properties['ssbi:customAttr'];
        const customAttr = ssbi ? JSON.parse(ssbi.replaceAll("'", '"')) : {};
        rootEl.innerHTML = '';
        rootEl.appendChild(el);
        const Profile = createVNode(
          h(condition, {
            customAttr: customAttr,
            type: 'bpmn:condition'
          })
        );

        // new Profile().$mount(el)
        render(Profile, el);
      }
    }
    class ConnectNodeModel extends HtmlNodeModel {
      setAttributes() {
        this.text.editable = false; // 禁止编辑文本
      }
      createId() {
        return 'ssbi-' + uuidv4();
      }
      initNodeData(data) {
        // if (data.text) {
        //   data.text.editable = false
        // }
        super.initNodeData(data);
        const width = 240;
        const height = 100;
        this.width = width;
        this.height = height;
        this.anchorsOffset = [
          [width / 2, 0],
          [0, height / 2],
          [-width / 2, 0],
          [0, -height / 2]
        ];
      }
    }
    return {
      view: ConnectNode,
      model: ConnectNodeModel
    };
  });
}
